﻿/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef UCOP_MACROS_H_
#define UCOP_MACROS_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

/* MAX, MIN */
#define UCOP_MIN(a, b)  ( (a) < (b) ? (a) : (b) )
#define UCOP_MAX(a, b)  ( (a) > (b) ? (a) : (b) )

/* Little-endian */
#define UCOP_LITTLEENDIAN 0
#define UCOP_BIGENDIAN    1

/* Assert */
#define UCOP_ASSERT(Expr)                   \
    if (!(Expr)) {                          \
        assert(false);                      \
    }                                       \

/* For-class */
#define DISALLOW_COPY_AND_ASSIGN(TypeName)  \
    TypeName(const TypeName&);              \
    void operator=(const TypeName&)

///////////////////////////////////////////////////////////////////////////////
/* For mutex lock */
#define UCOP_TYPE_LOCK_GURAD(Type, Var)                                    \
    LockGuard<Type> guard##Var(Var);                                       \

#define UCOP_TYPE_LOCK_GURAD_ASSERT(Type, Var)                             \
    LockGuard<Type> guard##Var(Var);                                       \
    if (!guard##Var.IsLocked()) {                                          \
        UCOP_ASSERT(false);                                                \
    }                                                                      \

#define UCOP_TYPE_LOCK_GURAD_ASSERT_RETURN(Type, Var)                      \
    LockGuard<Type> guard##Var(Var);                                       \
    if (!guard##Var.IsLocked()) {                                          \
        UCOP_ASSERT(false);                                                \
        return;                                                            \
    }                                                                      \

#define UCOP_TYPE_LOCK_GURAD_ASSERT_RETURN_VAR(Type, Var, Rtn)             \
    LockGuard<Type> guard##Var(Var);                                       \
    if (!guard##Var.IsLocked()) {                                          \
        UCOP_ASSERT(false);                                                \
        return Rtn;                                                        \
    }                                                                      \

#define UCOP_LOCK_GURAD(Var)                                               \
    UCOP_TYPE_LOCK_GURAD(Mutex, Var)

#define UCOP_LOCK_GURAD_ASSERT(Var)                                        \
    UCOP_TYPE_LOCK_GURAD_ASSERT(Mutex, Var)

#define UCOP_LOCK_GURAD_ASSERT_RETURN(Var)                                 \
    UCOP_TYPE_LOCK_GURAD_ASSERT_RETURN(Mutex, Var)

#define UCOP_LOCK_GURAD_ASSERT_RETURN_VAR(Var, Rtn)                        \
    UCOP_TYPE_LOCK_GURAD_ASSERT_RETURN_VAR(Mutex, Var, Rtn)


#endif // UCOP_MACROS_H_
